<!--
 * @Author: jiangyg email@jiangyg.com
 * @Date: 2022-08-14 15:31:12
 * @LastEditors: jiangyg email@jiangyg.com
 * @LastEditTime: 2022-11-20 08:01:53
 * @FilePath: /frontend/mall-frontend/src/views/modules/admin/system/user.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="mod-user">
    <data-table ref="dataTableRef" :criterias="criterias" :initLoad="true" name="user" title="用户管理" url="/user/select">
      <!-- ------------------------------- 查询条件 --------------------------------- -->
      <template v-slot:search="criterias">
        <dt-search-item label="检索内容" prop="content">
          <el-input v-model="criterias.content" maxlength="50" clearable style="width: 300px" placeholder="请输入用户ID/用户姓名/手机号/邮箱"></el-input>
        </dt-search-item>
      </template>
      <!-- --------------------------------- 工具栏 -------------------------------- -->
      <template v-slot:toolbar>
        <el-button-group class="toolbar-btngroup">
          <el-button v-if="admin.isAuth('admin:user:save')" @click="() => addDataEvent()" type="primary">
            新增
          </el-button>
          <el-button v-if="admin.isAuth('admin:user:save')" @click="() => updateDataEvent()" type="warning">
            修改
          </el-button>
          <el-button v-if="admin.isAuth('admin:user:delete')" @click="() => deleteDataEvent()" type="danger">
            删除
          </el-button>
        </el-button-group>
      </template>
      <!-- --------------------------------- 数据表格列 -------------------------------- -->
      <el-table-column fixed prop="username" align="center" label="用户名" width="160" />
      <el-table-column prop="fullname" align="center" label="用户姓名" width="160" />
      <el-table-column prop="departName" align="left" label="部门" width="160" />
      <el-table-column prop="roleNames" align="left" label="角色" width="180" />
      <el-table-column prop="mail" show-overflow-tooltip align="left" label="邮箱" width="160" />
      <el-table-column prop="phone" show-overflow-tooltip align="center" label="手机号" width="160" />
      <el-table-column prop="status" align="center" label="状态">
        <template #default="scope">
          <el-tag v-if="scope.row.status === '0'" size="small" type="danger">禁用</el-tag>
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="description" show-overflow-tooltip align="left" width="220" label="备注" />
    </data-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdateRef" @loadTableData="loadTableData()" />
  </div>
</template>

<script lang="ts" setup>
import admin from "@/utils/admin";
import { ref, nextTick } from "vue";

import AddOrUpdate from "./user-add-or-update.vue";

// 查询条件
const criterias = ref({ content: "" });

// 新增修改弹窗是否显示
const addOrUpdateVisible = ref(false);

// 弹窗引用
const addOrUpdateRef = ref();

// 数据表格引用
const dataTableRef = ref();

// 表格数据刷新
const loadTableData = () => {
  dataTableRef.value.loadTableData();
};

// 新增
const addDataEvent = async () => {
  addOrUpdateVisible.value = true;
  await nextTick();
  addOrUpdateRef.value.initForm("add");
};

// 修改
const updateDataEvent = async () => {
  // 验证并获取是否选择一条数据
  let data = admin.obtainOneSelectionTableData(dataTableRef.value);
  if (!data) {
    return;
  }
  addOrUpdateVisible.value = true;
  await nextTick();
  addOrUpdateRef.value.initForm("update", data);
};

// 删除
const deleteDataEvent = () => {
  admin.batchDeleteSelectionTableData(dataTableRef.value, '/user/delete');
};
</script>
